package com.orangelife.mobile.main.biz;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.common.jump.PropertyJumper;
import com.common.sp.UserSp;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.common.jump.JumpManager;
import com.common.jump.CommonJumper;
import com.datatrack.DataTrack;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.housepurchasing.activity.HouseShowingListActivity;
import com.orangelife.mobile.individual.activity.ExpressQueryActivity;
import com.orangelife.mobile.point.activity.MyPointActivity;
import com.orangelife.mobile.tehui.TehuiActivity;
import com.orangelife.mobile.tehui.TehuiDetailActivity;
import com.orangelife.mobile.util.LogE;
import com.orangelife.mobile.activities.activity.ActivityDetailActivity;
import com.orangelife.mobile.annoucement.activity.AnnoucementDetailActivity;
import com.orangelife.mobile.buyFresh.activity.BuyFreshDetailActivity;
import com.orangelife.mobile.surrounding.activity.HotGoodsDetailActivity;
import com.orangelife.mobile.complaints.activity.ComplainAdviceAddActivity;
import com.orangelife.mobile.complaints.activity.ComplainDetailActivity;
import com.orangelife.mobile.goodsrent.activity.RentListActivity;
import com.orangelife.mobile.hourlyEmployee.activity.OrderDetailActivity;
import com.orangelife.mobile.hourlyEmployee.activity.PlaceOrderActivity;
import com.orangelife.mobile.houserepair.activity.AddRepairActivity;
import com.orangelife.mobile.houserepair.activity.RepairDetailActivity;
import com.orangelife.mobile.individual.activity.MyOrderDetailActivity;
import com.orangelife.mobile.lookRepair.activity.LookForRepairActivity;
import com.orangelife.mobile.lookRepair.activity.LookForRepairDetailActivity;
import com.orangelife.mobile.main.activity.ApplyCertActivity;
import com.orangelife.mobile.web.WebActivity;
import com.orangelife.mobile.voucher.activity.MyVoucherListActivity;
import com.orangelife.mobile.myhouse.activity.HouseCertActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingCardListActivity;
import com.orangelife.mobile.parkingfee.activity.ParkingFeeListActivity;
import com.orangelife.mobile.redeem.activity.RedeemDetailActivity;
import com.orangelife.mobile.shop.activity.TraderDetailActivity;
import com.orangelife.mobile.surrounding.activity.SurroundingGoodListActivity;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.washcar.activity.WashCarActivity;
import com.sdu.didi.openapi.DiDiWebActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2015/11/17.
 */
public class JumpUtil {

    public static void jumpActivityWithoutHost(Context c, String path) {
        if (!TextUtils.isEmpty(path) && !path.contains("http")) {
            path = CurryApplication.getInstance().getServerURL() + path;
        }
        jumpActivity(c, path);
    }

    /**
     * 带数据采集的跳转方法
     * @param c
     * @param path
     */
    public static void jumpActivity(Context c, String  path,Map<String,Object> map){
        //path="http://10.168.7.4:8080/panda_web_wechat/testx/xxx.html";
        DataTrack.clickUrlTrack(path,map);
        jumpActivity(c, path, false, false);
    }
    public static void jumpActivity(Context c, String path) {
        jumpActivity(c, path, false, false);
    }

    public static void jumpActivity(Context c, String path, boolean needLoginID) {
        jumpActivity(c, path, needLoginID, false);
    }

    /**
     * @param c
     * @param path        WEB界面的跳转地址 或者 原生界面拦截地址
     * @param needLoginID 是否需要登陆ID
     * @param isNewTask   是否开启新栈，从JPush跳转的时候，需要此参数
     */
    public static void jumpActivity(Context c, String path, boolean needLoginID, boolean isNewTask) {
        LogE.E("JumpUtil jumpActivity webViewURL=" + path);
        if (TextUtils.isEmpty(path) || c == null) {
            return;
        }
        if (path.indexOf("//") == -1) {
            return;
        }
        Uri uri = Uri.parse(path);
        if (needLoginID && StringUtil.isContainBlank(UserSp.getLoginID())) {/*此次的跳转，是否需要登录ID*/
            CommonJumper.jumpToLogin(c, uri, isNewTask);
        } else if (uri.getScheme().equals("http") || uri.getScheme().equals("https")) {
            jumpToWebActivity(c, path);
        } else if (uri.getScheme().equals("orangelife")) {
            jumpToNativeActivity(c, uri, isNewTask);
        }
    }

    /** 跳转webview */
    private static void jumpToWebActivity(Context c, String path) {
        Bundle bundle = new Bundle();
        bundle.putString("uri", path);
        IntentHelper.getInstances().bundleWithExtra(c, WebActivity.class, bundle);
    }

    private static void jumpToNativeActivity(final Context c, Uri uri, boolean isNewTask) {
        startActivity(c, uri, isNewTask);
    }

    /** 判断是否在当前小区已认证（是否已有房屋） */
    private static boolean hasCert(Context c, String title, String hint) {
        if (!UserSp.hasCertInCurrentComm()) {
            Intent intent = new Intent();
            intent.putExtra("title", title);
            intent.putExtra("hint", hint);
            intent.setClass(c, ApplyCertActivity.class);
            c.startActivity(intent);
            return false;
        }
        return true;
    }

    private static void startActivity(final Context c, Uri uri, boolean isNewTask) {
        Intent intent = new Intent();
        String key = uri.getQueryParameter("mode");
        String id = uri.getQueryParameter("id");
        int flag = -1;
        if (!StringUtil.isBlank(id)) {
            intent.putExtra("id", id);
        }
        if (isNewTask) {
            flag = Intent.FLAG_ACTIVITY_NEW_TASK;
        }
        switch (key) {
            case "1":/*小区门禁*/
                break;
            case "2":/*停车管理*/
                IntentHelper.getInstances().intentWithExtra(c, ParkingCardListActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.PARKMNGM);
                break;
            case "3":/*物业报修*/
//                if (hasCert(c, "物业报修", "您尚未进行身份认证，无法进行报修，请先认证身份。")) {
                IntentHelper.getInstances().intentWithExtra(c, AddRepairActivity.class, null, null, flag);
//                }
                BLog.getInstance().click(BLog.PREPAIR);
                break;
            case "4":/*投诉建议*/
//                if (hasCert(c, "投诉建议", "您尚未进行身份认证，无法进行投诉，请先认证身份。")) {
                IntentHelper.getInstances().intentWithExtra(c, ComplainAdviceAddActivity.class, null, null, flag);
//                }
                BLog.getInstance().click(BLog.COMPLAIN);
                break;
            case "5":/*我要缴费*/
//                if (hasCert(c, "我要缴费", c.getResources().getString(R.string.hit_property_pay_cost))) {
                PropertyJumper.jumpToPropertyFeeList(c);
//                }
                BLog.getInstance().click(BLog.PMFEE);
                break;
            case "6":/*社区活动*/
                break;
            case "7":/*干洗*/
                break;
            case "8":/*房屋保修*/
                IntentHelper.getInstances().intentWithExtra(c, LookForRepairActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.REPAIR);
                break;
            case "9":/*宅急送*/
                break;
            case "10":/*家政*/
                IntentHelper.getInstances().intentWithExtra(c, PlaceOrderActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.CLEAN);
                break;
            case "11":/*家电清洗*/
                break;
            case "12":/*我的物业缴费*/
                PropertyJumper.jumpToPropertyHistoryList(c);
                BLog.getInstance().click(BLog.PMFEE);
                break;
            case "goodsDetail":/*餐桌管家详情*/
                IntentHelper.getInstances().intentWithExtra(c, BuyFreshDetailActivity.class, "id", id, flag);
                BLog.getInstance().click(BLog.FOOD, id);
                break;
            case "goodsOrder":/*商品订单详情*/
                IntentHelper.getInstances().intentWithExtra(c, MyOrderDetailActivity.class, "id", id, flag);
                break;
            case "scoreOrder":/*积分换购订单详情*/
                IntentHelper.getInstances().intentWithExtra(c, MyOrderDetailActivity.class, "id", id, flag);
                break;
            case "groupOrder":/*团购订单详情*/
                IntentHelper.getInstances().intentWithExtra(c, MyOrderDetailActivity.class, "id", id, flag);
                break;
            case "zbgoodsDetail":/*周边购详情*/
                IntentHelper.getInstances().intentWithExtra(c, HotGoodsDetailActivity.class, "id", id, flag);
                break;
            case "scoreDetail":/*积分换购详情*/
                IntentHelper.getInstances().intentWithExtra(c, RedeemDetailActivity.class, "id", id, flag);
                break;
            case "groupDetail":/*团购详情*/
                IntentHelper.getInstances().intentWithExtra(c, ActivityDetailActivity.class, "id", id, flag);
                break;
            case "repair":/*房屋报修详情*/
                IntentHelper.getInstances().intentWithExtra(c, LookForRepairDetailActivity.class, "id", id, flag);
                break;
            case "clean":/*房屋清洁详情*/
                IntentHelper.getInstances().intentWithExtra(c, OrderDetailActivity.class, "id", id, flag);
                break;
            case "mer":/*商家详情*/
                IntentHelper.getInstances().intentWithExtra(c, TraderDetailActivity.class, "id", id, flag);
                break;
            case "pmfee":/*物业缴费列表*/
                PropertyJumper.jumpToPropertyFeeList(c);
                BLog.getInstance().click(BLog.PMFEE);
                break;
            case "parking":/*停车缴费列表*/
                IntentHelper.getInstances().intentWithExtra(c, ParkingFeeListActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.PARKMNGM);
                break;
            case "parkingCard":/*停车管理列表*/
                IntentHelper.getInstances().intentWithExtra(c, ParkingCardListActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.PARKCARD);
                break;
            case "coupon":/*抵用券列表*/
                IntentHelper.getInstances().intentWithExtra(c, MyVoucherListActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.COUPON);
                break;
            case "28":/*公告详情*/
                IntentHelper.getInstances().intentWithExtra(c, AnnoucementDetailActivity.class, "id", id, flag);
                BLog.getInstance().click(BLog.MESSAGE, id);
                break;
            case "29":/*投诉详情*/
                IntentHelper.getInstances().intentWithExtra(c, ComplainDetailActivity.class, "id", id, flag);
                BLog.getInstance().click(BLog.COMPLAIN, id);
                break;
            case "30":/*物业报修详情*/
                IntentHelper.getInstances().intentWithExtra(c, RepairDetailActivity.class, "id", id, flag);
                break;
            case "home":/*主页*/
                CommonJumper.jumpToMain(c, flag, null);
                break;
            case "33":/*物品租借*/
                IntentHelper.getInstances().intentWithExtra(c, RentListActivity.class, null, null, flag);
                BLog.getInstance().click(BLog.RENT);
                break;
            case "32":/*身份认证,房屋认证*/
                IntentHelper.getInstances().intentWithExtra(c, HouseCertActivity.class, null, null, flag);
                break;
            case "38":/*商户详情*/
                Bundle bundle = new Bundle();
                bundle.putSerializable("merID", id);
                bundle.putSerializable("merchantName", String.valueOf(uri.getQueryParameter("title")));
                IntentHelper.getInstances().bundleWithExtra(c, SurroundingGoodListActivity.class, bundle, null, flag, -1);
                break;
            case "37":/*DD洗车*/
                IntentHelper.getInstances().intentWithExtra(c, WashCarActivity.class, null, null, flag);
                break;
            case "39":/*滴滴出行*/
                DiDiWebActivity.showDDPage(c, new HashMap<String, Object>());
                break;
            case "40":/*快递查询*/
                IntentHelper.getInstances().intentWithExtra(c, ExpressQueryActivity.class, null, null, flag);
                break;
            case "41":/*特惠详情*/
                IntentHelper.getInstances().intentWithExtra(c, TehuiDetailActivity.class, "id", id, flag);
                break;
            case "42":/*特惠*/
                IntentHelper.getInstances().intentWithExtra(c, TehuiActivity.class, "id", id, flag);
                break;
            case "43":/*积分明细*/
                IntentHelper.getInstances().intentWithExtra(c, MyPointActivity.class, null, null, flag);
                break;
            case "44":
                IntentHelper.getInstances().intentWithExtra(c, HouseShowingListActivity.class, null, null, flag);
                break;
            default:/*0:更多*/

                break;
        }
    }
}
